package com.yiwei.zxp.activity;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.FileProvider;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.yiwei.zxp.R;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.constant.IdCardBitmap;
import com.yiwei.zxp.util.ImageUtil;
import com.yiwei.zxp.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.net.URLEncoder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-5.
 */

public class IdcardIdentityActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;

    @BindView(R.id.pager)
    ViewPager pager;
    private static final int CAMERA_REQUEST_CODE = 2;
    private Unbinder unbiner;
    private int takeType;
    private static final String DEFAULT_NAME1 = "front_image.jpg";
    private static final String DEFAULT_NAME2 = "back_image.jpg";
    private static final String DEFAULT_NAME3 = "people_image.jpg";

    private File mTempCameraFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_balance_withdraw);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
        EventBus.getDefault().register(this);

    }

    private void init() {
        txtTitle.setText("身份认证");
//        fragmentOne = new FragmentIdcardOne();
//        fragmentTwo = new FragmentIdcardTwo();
//        fragmentThree = new FragmentIdcardThree();
//        Fragment[] fragments = {fragmentOne, fragmentTwo, fragmentThree};
//        String[] titles = new String[]{"首页", "发现", "我的"};
//        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
//            @Override
//            public Fragment getItem(int position) {
//                return fragments[position];
//            }
//
//            @Override
//            public int getCount() {
//                return fragments.length;
//            }
//        });
    }


//    public void getUploadFace(Bitmap img) {
//        if (!Network.checkNetwork(this)) {
//            ToastUtil.show(this, "网络连接失败");
//            return;
//        }
//        loadingDialog.show();
//        UploadBean item = new UploadBean();
//        item.img = ImageUtil.Bitmap2StrByBase64(img, 70);
//
//        Api.api_service.getUploadFace(item).compose(RxLifeUtil.checkOn(this)).
//                doFinally(() -> {
//                    try {
//                        loadingDialog.dismiss();
//                    } catch (NullPointerException e) {
//
//                    }
//                }).
//                subscribe(result -> {
//                    face = result.datas.url;
//                    ToastUtil.makeText(this, "上传成功");
//                    goNext(2);
//                });
//    }


    private boolean isVerify = false;

    /**
     * 获取认证结果查询
     */


    public void startTakePhoto(int type) {
        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                takeType = type;
                if (type == 1) {
                    startActivity(DEFAULT_NAME1);
                }
                if (type == 2) {
                    startActivity(DEFAULT_NAME2);
                } else if (type == 3) {
                    goToTakePhoto();
                }
            }
        }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }


    private void startActivity(String paramString) {
        Intent localIntent = new Intent(this, CameraActivity.class);
        localIntent.putExtra("path", "/sdcard/pictures");
        localIntent.putExtra("name", paramString);
        localIntent.putExtra("type", "idcard");
        startActivityForResult(localIntent, 100);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (resultCode == -1) {
                try {

                    if (takeType == 1) {
                        if (IdCardBitmap.bitmap != null) {
//                            fragmentOne.setPhoto(takeType, IdCardBitmap.bitmap);
                        }
                    } else if (takeType == 2) {

                        if (IdCardBitmap.bitmap != null) {
//                            fragmentOne.setPhoto(takeType, IdCardBitmap.bitmap);
                        }
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if (requestCode == CAMERA_REQUEST_CODE && resultCode == -1) {
            if (mTempCameraFile.exists()) {
                try {
//                    fragmentOne.setPhoto(takeType, ImageUtil.getimage(mTempCameraFile.getAbsolutePath(), 750, 500, 250));

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }
    }

    private String name;
    private String idno;
    private String face;

    public void setIdcardInfo(String name, String idno) {
        this.name = name;
        this.idno = idno;
    }

//    public void setFaceInfo(String face) {
//        this.face = face;
//    }


    private void goToTakePhoto() {
        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                if (Build.VERSION.SDK_INT >= 23) {
                    Intent cameraIntent;
                    if (Build.VERSION.SDK_INT < 24) {
                        cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempCameraFile()));
                        startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
                    } else {
                        cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        Uri apkUri = FileProvider.getUriForFile(IdcardIdentityActivity.this, "com.yiwei.zxp.fileprovider", getTempCameraFile());
                        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, apkUri);
                        startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
                    }
                } else {
                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempCameraFile()));
                    startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
                }
            }
        }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    private File getTempCameraFile() {
        if (mTempCameraFile == null)
            mTempCameraFile = ImageUtil.getTempMediaFile();
        return mTempCameraFile;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        pager.removeAllViews();
        if (IdCardBitmap.bitmap != null) {
            IdCardBitmap.bitmap.recycle();
            IdCardBitmap.bitmap = null;
        }

        EventBus.getDefault().unregister(this);
        unbiner.unbind();
        unbiner = null;

    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
